/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-14
 * Time: 14:42
 */
public class MySignalList {

    public ListNode head;//头结点

    //结点
    static class ListNode {
        int val;//结点的值
        ListNode next;//指向下一个结点

        public ListNode(int val) {
            this.val = val;
        }

    }

    //头插
    public void addFirst(int val) {
        ListNode NewNode = new ListNode(val);
        if (head == null) {
            //头结点为空(最开始,没有结点)
            head = NewNode;
            return;
        }

        NewNode.next = head;
        head = NewNode;

    }

    //尾插
    public void addLast(int val) {
        ListNode NewNode = new ListNode(val);
        if (head == null) {
            //头结点为空
            head = NewNode;
            return;
        }

        ListNode tail = head;
        while (tail.next != null) {
            tail = tail.next;
        }
        tail.next = NewNode;


    }

    //头删
    public void deleteFirst() {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            head = null;
            return;
        }
        ListNode cur = head;

        head = head.next;

        cur.next = null;

    }

    //尾删
    public void deleteLast() {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            head = null;
            return;
        }

        ListNode cur = head;
        ListNode pre = null;
        while (cur.next != null) {
            pre = cur;
            cur = cur.next;
        }
        pre.next = null;

    }

    //打印链表
    public void write() {
        ListNode cur = head;
        if (head == null) {
            System.out.println("null");
            return;
        }
        while (cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("null");
    }



}
